#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: migrate_scene.py
# @time: 2021/4/8 13:12
# @desc:
import os
from datetime import datetime

import pymongo

from .views.configs import *
from .utils.md5_tools import getFileMd5
from .utils.mongo_tools import total_number_generator_only_for_old_clamp, total_number_generator, retrieve_all_mongo_values
from .utils.oss.oss import upload_file_to_oss
from .utils.utils import read_json

myclient = pymongo.MongoClient(MONGO_URL)
mydb = myclient["do_center"]
scene_connection = mydb["scene"]

scene = read_json("script/scene.json")

scene_list = [{i: scene[i]} for i in scene.keys()]

new_scene_list = []

for i in scene.keys():
    temp_dict = {
        "number": str(NUMBER_SCENE_START + total_number_generator(
            database_name="do_center",
            collection_name="scene",
        )),
        "name": i,
        "type": "6",
        "desc": scene[i]["desc"],
        "historyClamp": [],
        "time": datetime.now().strftime("%Y-%m-%d %H:%M"),
    }
    for j in scene[i]["clamp"]:
        result = retrieve_all_mongo_values(
            filter_conditions={"file.stpMD5": {"$eq": j}},
            database_name="do_center",
            collection_name="history_clamp",
        )
        if len(result) > 0:
            temp_dict["historyClamp"].append(result[0]["number"])

    scene_connection.insert_one(temp_dict)
